//client.c
#include "../include/func.h"

int main(int argc, char *argv[]) {
    // ./client ../conf/client.conf
    ARGS_CHECK(argc, 2);

    char ip[20] = {0};
    char port[10] = {0};
    getClientConf(argv[1], ip, port);

    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    ERROR_CHECK(sockfd, -1, "socket");

    // setsockopt
    int flag = 1;
    setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));

    // addr
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr(ip);
    addr.sin_port = htons(atoi(port));

    int ret = connect(sockfd, (struct sockaddr*)&addr, sizeof(addr));
    ERROR_CHECK(ret, -1, "connect");
    printf("connecting server...\n");

    // 初始化共享资源
    userInfo_t user_info;
    memset(&user_info, 0, sizeof(userInfo_t));

    // 用户认证菜单
    int auth_choice = 0;
    while (1) {
        printf("\n=== 网盘系统 ===\n");
        printf("1. 注册\n");
        printf("2. 登录\n");
        printf("3. 注销账户\n");
        printf("4. 退出\n");
        printf("请选择: ");
        scanf("%d", &auth_choice);
        getchar(); // 清除换行符

        switch (auth_choice) {
        case 1:{
                   user_register(sockfd, &user_info);
                   // 不管成功与否都再次进入循环
                   break;}
        case 2:{
                   ret = user_login(sockfd, &user_info);
                   if (ret == 0) {
                       printf("登录成功！\n");
                       goto command_loop; // 登录成功后进入命令循环
                   }
                   break;}
        case 3:{
                   user_cancel(sockfd, &user_info);
                   // 不管成功与否都再次进入循环
                   break;}
        case 4:
               close(sockfd);
               return 0;
        default:
               printf("无效选择！\n");
               break;
        }
    }

command_loop:
    // 命令处理循环
    // 做一次pwd
    while (1) {
        printf("\n[%s] $ ", user_info.current_dir);

        char line[128] = "";
        cmd_t cmd;
        memset(&cmd, 0, sizeof(cmd_t));

        // 读取命令
        if (fgets(line, sizeof(line), stdin) == NULL) {
            break;
        }
        line[strlen(line) - 1] = '\0';

        // 检查退出命令
        if (!strcmp(line, "quit")) {
            break;
        }
        if (strlen(line) == 0) {
            continue;
        }

        // 解析命令
        init_cmd(line, &cmd);

        // 检查命令合法性并执行
        int tag = 0;
        if (cmd_check(&cmd, &tag) != 0) {
            printf("无效命令。请重新输入。\n");
            continue;
        }

        // 根据命令类型执行相应操作
        switch (tag) {
        case TAGPWD:
            handle_pwd(sockfd);
            break;
        case TAGLS:
            handle_ls(sockfd);
            break;
        case TAGCD:
            handle_cd(sockfd, &cmd);
            break;
        case TAGMKDIR:
            handle_mkdir(sockfd, &cmd);
            break;
        case TAGRMDIR:
            handle_rmdir(sockfd, &cmd);
            break;
        case TAGREMOVE:
            handle_remove(sockfd, &cmd);
            break;
        case TAGGETS:
            handle_gets(sockfd, &cmd);
            break;
        case TAGPUTS:
            handle_puts(sockfd, &cmd);
            break;
        default:
            printf("未知命令类型\n");
            break;
        }
    }

    close(sockfd);

    return 0;
}

